package com.tmsps.ne4Weixin.utils;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tmsps.ne4Weixin.config.WxConfig;

/**
 * 
 * @author zhangwei 396033084@qq.com WeixinSupportUtil
 */
public class WeixinUtil {
	protected static Logger log = LoggerFactory.getLogger(WeixinUtil.class);

	/**
	 * @author zhangwei 396033084@qq.com 验证微信信息
	 */
	public static boolean isCheck(HttpServletRequest request, String token) {
		String signature = request.getParameter("signature");
		String timestamp = request.getParameter("timestamp");
		String nonce = request.getParameter("nonce");
		return SignUtil.checkSignature(token, signature, timestamp, nonce);
	}

	/**
	 * @author zhangwei 396033084@qq.com 解密微信接收到的消息
	 */
	public static String decryptMsg(HttpServletRequest request, WxConfig wc, String ciphertext) {
		if (wc.getIssafe()) {
			String msgSignature = request.getParameter("msg_signature");
			String timeStamp = request.getParameter("timestamp");
			String nonce = request.getParameter("nonce");
			log.debug("timeStamp:{},nonce:{},msgSignature:{},ciphertext:{}", timeStamp, nonce, msgSignature, ciphertext);
			return MsgEncryptUtil.decrypt(ciphertext, timeStamp, nonce, msgSignature, wc);
		} else {
			return ciphertext;
		}
	}

	public static String handelMessage(HttpServletRequest request) {
		return "";
	}
}
